<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="truckTypeModel" :rules="formtruckTypes"
      ref="TruckTypeForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="车型名称" prop="name" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="truckTypeModel.name" placeholder="请输入车型名称"></el-input></el-col>
      </el-form-item>
       <el-form-item label="车型载重" prop="allowableLoad" label-width="120px">
        <el-col :span="16">
         <el-input :maxlength="32" v-model="truckTypeModel.allowableLoad" placeholder="请输入车型载重"></el-input>
        </el-col>
      </el-form-item>
       <el-form-item label="车型体积" prop="allowableVolume" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="truckTypeModel.allowableVolume" placeholder="请输入车型体积"></el-input></el-col>
      </el-form-item>
     <el-form-item label="长" prop="measureLong" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="truckTypeModel.measureLong" placeholder="请输入长"></el-input></el-col>
      </el-form-item>
       <el-form-item label="宽" prop="measureWidth" label-width="120px">
        <el-col :span="16">
         <el-input :maxlength="32" v-model="truckTypeModel.measureWidth" placeholder="请输入宽"></el-input>
        </el-col>
      </el-form-item>
       <el-form-item label="高" prop="measureHigh" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="truckTypeModel.measureHigh" placeholder="请输入高"></el-input></el-col>
      </el-form-item>
      <el-form-item label="适用货品类型" prop="goodsTypeIds" label-width="120px">
        <el-col :span="16">
            <el-select v-model="truckTypeModel.goodsTypeIds" multiple  >
              <el-option
                    v-for="item in parentGoodsType"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
            </el-option>
            </el-select>
        </el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('TruckTypeForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import modelsApi from '@/api/models.js'
export default {
   name: "truckType-edit",
   props: ['parentGoodsType'],
    data() {
    return {
      loading: false,
      truckTypeModel: {
        name:"",
        allowableLoad: '',
        allowableVolume:'',
        measureLong: '',
        measureWidth:'',
        measureHigh:'',
        goodsTypeIds:[]
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formtruckTypes: {
         name: [
          { required: true, message: '请输入车型名称', trigger: 'blur' },
        ],
        goodsTypeIds:[{
          required: true, message: '请输入使用货品类型', trigger: 'blur'
        }]
      }
    }
 },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveTruckType()
          } else {
            this.doUpdateTruckType()
          }
        } else {
          return false
        }
      })
    },
    doUpdateTruckType() {
      this.loading = true
      console.log(this.truckTypeModel)
      modelsApi.update(""+this.truckTypeModel.id,this.truckTypeModel)
      .then(response => {
        console.log(response)
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    doSaveTruckType() {
      this.loading = true
      console.log(this.truckTypeModel)
      modelsApi.save(this.truckTypeModel)
      .then(res => {
        console.log(res)
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(err => {
        console.error(err);
      })

    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['TruckTypeForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
   mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditTruckTypeDialog', function(truckType) {
        console.log(truckType)
        this.truckTypeModel = truckType
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '编辑车型'
        this.dialogProps.visible = true
      })
      this.$on('openAddTruckTypeDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '新增车型'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style>

</style>
